using System.Web;

namespace suanpan.API.Components
{
    public class UriRewriteModule : IHttpModule
    {        

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new System.EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, System.EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            //only want to rewrite if it's a call to the api
            if ( !context.Request.Url.Host.Contains("api.") )
                return;
            //allow requests to the .svc resource
            if ( context.Request.Path.Contains(".svc") || context.Request.Path.Contains(".") )
                return;
            context.Request.Headers.Add("X-REWRITE-URL", context.Request.Url.AbsolutePath);
            context.RewritePath("~/chess.svc", context.Request.Path, context.Request.QueryString.ToString(), false);
        }

        public void Dispose()
        {
            return;
        }

    }
}